package com.myway.controller;

import com.myway.dto.beans.PageQuery;
import com.myway.dto.common.JsonData;
import com.myway.dto.param.ContentParam;
import com.myway.pojo.SysPortalNews;
import com.myway.service.ContentService;
import com.myway.service.FootService;
import com.myway.service.SysPortalNewsService;
import com.myway.vo.ContentVo;
import com.myway.vo.NewsDetailOnlyVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by yufei on 2018/1/17.
 */
@Controller
@Slf4j
@RequestMapping("/en/content")
public class ContentControllerEn {
    @Autowired
    private ContentService contentService;

    @Autowired
    private SysPortalNewsService sysPortalNewsService;

    @Autowired
    private FootService footService;
    /**
     * 跳转服务助手
     * @param param
     * @return
     */
    @RequestMapping("")
    public ModelAndView page(@Param("param") ContentParam param ,HttpServletRequest request){
        ContentVo contentVo = contentService.page(param);
        ModelAndView model = new ModelAndView();
        HttpSession session = request.getSession();
        session.setAttribute("type",param.getType());
        model.setViewName("en/news");
        model.addObject("contentVo",contentVo);
        return model;
    }

    @RequestMapping("/detail/info")
    public ModelAndView itemDetailPage(@Param("id") Long id,@Param("type") String type){
        //查询当前内容
        SysPortalNews sysPortalNews = sysPortalNewsService.getPageById(id);
        //查询上一篇内容
        SysPortalNews preSysPortalNews = sysPortalNewsService.getPrePageById(sysPortalNews.getOperateTime(),type);
        //查询下一篇内容
        SysPortalNews nextSysPortalNews = sysPortalNewsService.getNextPageById(sysPortalNews.getOperateTime(),type);

        NewsDetailOnlyVo vo = NewsDetailOnlyVo.builder().sysPortalNews(sysPortalNews)
                .preSysPortalNews(preSysPortalNews)
                .nextSysPortalNews(nextSysPortalNews).build();
        ModelAndView model = new ModelAndView();
        model.setViewName("en/newsdetailonly");
        model.addObject("newsDetail",vo);
        return model;
    }

    /**
     * 判断内容size，跳转不同页面
     * 如果内容列表sise为1则返回nesdetailonly页面
     * 如果内容列表size>1则返回newsonly页面
     */
    @RequestMapping("/list")
    public ModelAndView contentListOrDetailView(@Param("type") String type){
        int count = sysPortalNewsService.count(type);
        ModelAndView model = new ModelAndView();
        if(count>1){
            //返回内容列表页面
            model.setViewName("en/newsonly");
            //将内容的type返回前端
            model.addObject("type",type);
            //将内容总数返回前端
            model.addObject("count", count);
        }else if(count == 1){
            //返回内容展示页面
            model.setViewName("en/newsdetailonly");
            //查询唯一的数据
            List<SysPortalNews> sysPortalNewses = sysPortalNewsService.itemDetailPage(type);
            NewsDetailOnlyVo vo = NewsDetailOnlyVo.builder().sysPortalNews(sysPortalNewses.get(0)).build();
            //将内容返回前端
            model.addObject("newsDetail",vo);
        }else{
            //如果没有内容信息，返回默认页面
            model.setViewName("en/contentDefault");
        }
        return model;
    }
    /**
     * 分页查询内容
     */
    @RequestMapping("/page")
    @ResponseBody
    public JsonData pageContent(PageQuery page,String type){
        List<SysPortalNews> list = sysPortalNewsService.page(page, type);
        return JsonData.success(list);
    }
}
